package com.cy.pj.modules.controller;

import com.alibaba.fastjson.JSON;
import com.cy.pj.common.vo.JsonResult;
import com.cy.pj.common.web.domain.BaseController;
import com.cy.pj.modules.entity.Order;
import com.cy.pj.modules.service.IOrderService;
import com.cy.pj.modules.vo.OrderSkusVo;
import com.cy.pj.modules.vo.OrderVo;
import com.cy.pj.modules.vo.SkuVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 【请填写功能名称】Controller
 *
 * @author kiven
 * @date 2023-03-07
 */
@Controller
@RequestMapping("/order")
public class OrderController extends BaseController
{
    private String prefix = "modules/order";

    @Autowired
    private IOrderService orderService;

    @RequiresPermissions("order:view")
    @GetMapping()
    public String order()
    {
        return prefix + "/order";
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @RequiresPermissions("order:list")
    @GetMapping("/list")
    @ResponseBody
    public JsonResult list(Order order, Integer pageCurrent)
    {

        return new JsonResult(orderService.selectOrderList(order, pageCurrent));
    }
    //
    ///**
    // * 导出【请填写功能名称】列表
    // */
    //@RequiresPermissions("pj:order:export")
    //@PostMapping("/export")
    //@ResponseBody
    //public AjaxResult export(Order order)
    //{
    //    List<Order> list = orderService.selectOrderList(order);
    //    ExcelUtil<Order> util = new ExcelUtil<Order>(Order.class);
    //    return util.exportExcel(list, "order");
    //}

    /**
     * 新增【请填写功能名称】
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存【请填写功能名称】
     */
    @PostMapping("/sumPrice")
    @ResponseBody
    public JsonResult sumPrice(@RequestBody  String json)
    {
        System.out.println(json);
        OrderVo orderVo = JSON.parseObject(json, OrderVo.class);
        return new JsonResult(orderService.sumPrice(orderVo));
    }



    /**
     * 新增保存【请填写功能名称】
     */
    @RequiresPermissions("order:add")
    @PostMapping("/add")
    @ResponseBody
    public JsonResult addSave(@RequestBody  String json)
    {
        System.out.println(json);
        Map<String,Object> map = JSON.parseObject(json, Map.class);
        String skus = JSON.toJSONString(map.get("skus"));
        List<SkuVo> list = JSON.parseObject(skus,List.class);

        OrderSkusVo orderVo = JSON.parseObject(json, OrderSkusVo.class);
        orderVo.setOrderItems(list);
        return new JsonResult(orderService.insertOrder(orderVo));
    }
    /**
     * 修改【请填写功能名称】
     */
    @GetMapping("/edit")
    public String edit( String id, ModelMap mmap)
    {
        Order order = orderService.selectOrderById(id);
        mmap.put("order", order);
        return prefix + "/order_edit";
    }

    /**
     * 修改保存【请填写功能名称】
     */
    @RequiresPermissions("order:update")
    @PostMapping("/edit")
    @ResponseBody
    public JsonResult editSave(Order order)
    {
        return new JsonResult(orderService.updateOrder(order));
    }

    /**
     * 删除【请填写功能名称】
     */
    @RequiresPermissions("order:delete")
    @PostMapping( "/remove")
    @ResponseBody
    public JsonResult remove(String ids)
    {
        return new JsonResult(orderService.deleteOrderByIds(ids));
    }
}
